//
//  FirstView.m
//  相册罚写1
//
//  Created by dllo on 15/11/17.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "FirstView.h"
#import "FirstScrollView.h"

@interface FirstView ()<UIScrollViewDelegate>

@property (nonatomic, retain) UIScrollView *scrollV;
@property (nonatomic, retain) UIPageControl *pageC;
@property (nonatomic, assign) NSInteger pageNow;

@end

@implementation FirstView

- (void)dealloc
{
    [_scrollV release];
    [_pageC release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews];
    }
    return self;
}

- (void)createSubviews
{
    self.backgroundColor = [UIColor whiteColor];
    
    self.scrollV = [[UIScrollView alloc] initWithFrame:self.frame];
    self.scrollV.contentSize = CGSizeMake(self.frame.size.width * 21, 0);
    self.scrollV.contentOffset = CGPointMake(self.frame.size.width, 0);
    self.scrollV.pagingEnabled = YES;
    self.scrollV.showsHorizontalScrollIndicator = YES;
    self.scrollV.delegate = self;
    [self addSubview:self.scrollV];
    [_scrollV release];
    
    for (NSInteger i = 0; i < 21; i++) {
        FirstScrollView *firstV = [[FirstScrollView alloc] initWithFrame:CGRectMake(self.frame.size.width * i, 0, self.frame.size.width, self.frame.size.height) index:i];
        [self.scrollV addSubview:firstV];
        [firstV release];
        
     
    }
    
    self.pageC = [[UIPageControl alloc] initWithFrame:CGRectMake(50, 100, 300, 30)];
    self.pageC.numberOfPages = 19;
    [self.pageC addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:self.pageC];
    [_pageC release];
    
    
    
    
    
}

- (void)pageAction:(UIPageControl *)sender
{

    self.scrollV.contentOffset = CGPointMake(self.frame.size.width * (sender.currentPage + 1), 0);

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{

    if (self.scrollV == scrollView) {
        
        NSInteger currentPage = self.scrollV.contentOffset.x / self.frame.size.width;
        
        if (0 == currentPage) {
            self.scrollV.contentOffset = CGPointMake(self.frame.size.width * 19, 0);
            currentPage = 19;
        } else if (20 == currentPage) {
            self.scrollV.contentOffset = CGPointMake(self.frame.size.width, 0);
            currentPage = 1;
        }
        
        if (currentPage != self.pageNow) {
            [[self.scrollV.subviews objectAtIndex:self.pageNow] setZoomScale:1];
            self.pageC.currentPage = currentPage - 1;
            self.pageNow = currentPage;
        }
        
        
        
        
        
        
        
    }


}

@end
